<?php
namespace App\Http\Controllers\Test;

use App\Http\Controllers\Controller;
use App\Model\ChenAppointment as Model;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpWord\PhpWord;

class OfficeWork extends Controller
{
    public static function makeExcel2($data,$filename=null)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
        if (empty($data)) {
        } else {
            /*$header = Arr::pluck($data[0], 'label');
            $sheet->fromArray($header, NULL, 'A1');*/
            foreach ($data as $k => $v) {
                $value = Arr::pluck($v, 'value');
                $sheet->fromArray($value, NULL, 'A'.(1+$k));
            }
        }
        $filename = $filename ?: time().'.xlsx';
        $filename = $filename . '-' . count($data);
        $filename = basename($filename, '.xlsx').'.xlsx';
        // $temp_file = tempnam(sys_get_temp_dir(), 'PHPWord');
        $path = __DIR__.'/20201209/';
        if (!file_exists($path)) {
            mkdir($path);
        }
        $temp_file = $path.$filename;
        $writer->save($temp_file);
        // return response()->download($temp_file, $filename);
    }

    public function makeExcel($data,$filename=null)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
        if (empty($data)) {
        } else {
            // IOFactory::
            $header = Arr::pluck($data[0], 'label');
            $sheet->fromArray($header, NULL, 'A1');
            foreach ($data as $k => $v) {
                $value = Arr::pluck($v, 'value');
                $sheet->fromArray($value, NULL, 'A'.(2+$k));
            }
        }
        $filename = $filename ?: time().'.xlsx';
        $filename = basename($filename, '.xlsx').'.xlsx';
        $temp_file = tempnam(sys_get_temp_dir(), 'PHPWord');
        $writer->save($temp_file);
        return response()->download($temp_file, $filename);
    }

    public function makeDocx($data, $filename=null)
    {
        $phpWord = new PhpWord();
        $phpWord->setDefaultFontSize(12);
        $phpWord->setDefaultParagraphStyle([
            'lineHeight' => 1.75,
            'spaceAfter' => 10,
        ]);
        $labelStyle = ['bold'=>true];

        $section = $phpWord->addSection();
        foreach ($data as $k=>$v) {
            $texy = $section->addTextRun();
            $texy->addText($v['label'].'：', $labelStyle);
            $texy->addText($v['value']);
        }

        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $filename = $filename ?: time().'.docx';
        $filename = basename($filename, '.docx').'.docx';
        $temp_file = tempnam(sys_get_temp_dir(), 'PHPWord');
        $objWriter->save($temp_file);
        return response()->download($temp_file, $filename);
    }

}

